/**
 * Package: Timers
 */

// Timer Prescaler Variables
#define PS_1      1
#define PS_8      2
#define PS_64     3
#define PS_256    4
#define PS_1024   5

#ifdef MEGA32
#define PS0_1      1
#define PS0_8      2
#define PS0_64     3
#define PS0_256    4
#define PS0_1024   5
#endif

#ifdef MEGA128
#define PS0_1      1
#define PS0_8      2
#define PS0_32     3
#define PS0_64     4
#define PS0_128    5
#define PS0_256    6
#define PS0_1024   7
#endif

#ifdef MEGA32

/// <summary>Initializes Counter0. Counter0 gets incremented at every positive signal edge at Input Mega32:T0 (PIN1).</summary>
void Timer_T0CNT            $opc(0x04)(void);

/// <summary>The value of Counter0 is read. If there was an overflow a value of 0xff is returned.</summary>
/// <returns>Counter value.</returns>
byte Timer_T0GetCNT         $opc(0x05)(void);

#endif

/// <summary>The value of Counter1 is read. If there was an overflow a value of 0xffff is returned.</summary>
/// <returns>Counter value.</returns>
word Timer_T1GetCNT         $opc(0x06)(void);

/// <summary>Initializes Counter1. Counter1 gets incremented at every positive signal edge at Input Mega32: PortB.1 (PIN2) Mega128: PortD.6 (X2_15).</summary>
void Timer_T1CNT            $opc(0x07)(void);

/// <summary>Initializes Counter1. Counter1 gets incremented at every positive signal edge at Input Mega32: PortB.1 (PIN2) Mega128: PortD.6 (X2_15). After the limit is reached an interrupt is triggered. An appropriate Interrupt Service Routine must be specified.</summary>
/// <param name="limit"></param>
void Timer_T1CNT_Int        $opc(0x08)(word limit);

/// <summary>The frequency generation is started with the already set parameters. The prescaler must be given again.</summary>
/// <param name="prescaler">Prescaler.</param>
void Timer_T0Start          $opc(0x09)(byte prescaler);

/// <summary>The frequency generation gets stopped. The output signal can be 0 or 1, dependent on the last state. Only the clock generation is stopped, all other settings stay the same.</summary>
void Timer_T0Stop           $opc(0x0A)(void);

/// <summary>Initializes Timer0 for frequency generation. Parameters are period duration and prescaler. The output signal is generated at Mega32: PortB.3 (PIN4), Mega128: PortB.4 (X1_4). The frequency generation is started automatically. There is an extended prescaler definition for the Mega128.</summary>
/// <param name="period">Period duration.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T0FRQ            $opc(0x0B)(byte Period,byte PS);

/// <summary>Initializes Timer0 with given prescaler and pulse width. The output signal is generated at Mega32: PortB.3 (PIN4), Mega128: PortB.4 (X1_4). There is an extended prescaler definition for the Mega128.</summary>
/// <param name="PW">Pulse width.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T0PWM            $opc(0x0C)(byte PW,byte PS);

/// <summary>Sets a new pulse width for Timer0 without changing the prescaler.</summary>
/// <param name="PW">Pulse width.</param>
void Timer_T0PW             $opc(0x0D)(byte PW);

/// <summary>The frequency generation is started with the already set parameters. The prescaler must be given again.</summary>
/// <param name="prescaler">Prescaler.</param>
void Timer_T1Start          $opc(0x0E)(byte prescaler);

/// <summary>The frequency generation gets stopped. The output signal can be 0 or 1, dependent on the last state. Only the clock generation is stopped, all other settings stay the same.</summary>
void Timer_T1Stop           $opc(0x0F)(void);

/// <summary>Initializes Timer1 for frequency generation. Parameters are period duration and prescaler. The output signal is generated at Mega32: PortD.5 (PIN19). Mega128: PortB.5 (X1_3). The frequency generation is started automatically. There is an extended prescaler definition for the Mega128.</summary>
/// <param name="period">Period duration.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T1FRQ            $opc(0x10)(word Period,byte PS);

/// <summary>Initializes Timer1 for frequency generation. Parameters are period duration, prescaler and phase shift. The output signal is generated at Mega32: PortD.5 (PIN19). Mega128: PortB.5 (X1_3). The frequency generation is started automatically. There is an extended prescaler definition for the Mega128. The phase shift must be smaller than half the period.</summary>
/// <param name="period">Period duration.</param>
/// <param name="skew">Phase shift.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T1FRQX           $opc(0x11)(word Period,word skew,byte PS);

/// <summary>Initializes Timer0 with given prescaler and pulse width. The output signal is generated at Mega32: PortD.5 (PIN19), Mega128: PortB.5 (X1_3). There is an extended prescaler definition for the Mega128.</summary>
/// <param name="period">Period duration.</param>
/// <param name="PW0">Pulse width.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T1PWM            $opc(0x12)(word Period,word PW0,byte PS);

/// <summary>Sets a new pulse width (Channel A) for Timer1 without changing the prescaler.</summary>
/// <param name="PW0">Pulse width.</param>
void Timer_T1PWA            $opc(0x13)(word PW0);

/// <summary>Initializes Timer_1 with given period duration, prescaler, pulse width for channel A and B. The output signal is generated at Mega32: PortD.4 (PIN18) and PortD.5 (PIN19). Mega128: PortB.5 (X1_3) and PortB.6 (X1_2).</summary>
/// <param name="period">Period duration.</param>
/// <param name="PW0">Pulse width channel A.</param>
/// <param name="PW1">Pulse width channel B.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T1PWMX           $opc(0x14)(word Period,word PW0,word PW1,byte PS);

/// <summary>Sets a new pulse width (Channel B) for Timer1 without changing the prescaler.</summary>
/// <param name="PW1">Pulse width.</param>
void Timer_T1PWB            $opc(0x15)(word PW1);

/// <summary>Initializes Timer_0 with a prescaler and a timer interval value. After the timing interval has expired the Timer_0 Interrupt is triggered. There is an extended prescaler definition for the Mega128.</summary>
/// <param name="time">Time period after which the interrupt is triggered.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T0Time           $opc(0x39)(byte Time, byte PS);

/// <summary>Initializes Timer_1 with a prescaler and a timer interval value (16bit). After the timing interval has expired Timer_1 Interrupt is triggered. There is an extended prescaler definition for the Mega128.</summary>
/// <param name="time">Time period after which the interrupt is triggered.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T1Time           $opc(0x3A)(word Time, byte PS);

/// <summary>Defines if pulse width measurement or period measurement should be done. Then it initializes Timer_1 and sets the prescaler.</summary>
/// <param name="Mode">0 = pulse width measurement, 1 = period measurement.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T1PM             $opc(0x3B)(byte Mode, byte PS);

/// <summary>Returns the result of the measurement.</summary>
/// <returns>Result of measurement.</returns>
word Timer_T1GetPM          $opc(0x3C)(void);

#ifdef MEGA128

/// <summary>The value of Counter1 is read. If there was an overflow a value of 0xffff is returned.</summary>
/// <returns>Counter value.</returns>
word Timer_T3GetCNT         $opc(0x62)(void);

/// <summary>Initializes Counter3. Counter3 gets incremented at every positive signal edge at Input PortE.6 (X1_10).</summary>
void Timer_T3CNT            $opc(0x63)(void);

/// <summary>Initializes Counter_3. Counter_3 gets incremented at every positive signal edge at Input PortE.6 (X1_10). After the limit is reached an interrupt ("Timer3 CompareA" - define: INT_TIM3CMPA ) is triggered. An appropriate Interrupt Service Routine must be specified.</summary>
/// <param name="limit"></param>
void Timer_T3CNT_Int        $opc(0x64)(word limit);

/// <summary>The frequency generation is started with the already set parameters. The prescaler must be given again.</summary>
/// <param name="prescaler">Prescaler.</param>
void Timer_T3Start          $opc(0x65)(byte PS);

/// <summary>The frequency generation gets stopped. The output signal can be 0 or 1, dependent on the last state. Only the clock generation is stopped, all other settings stay the same.</summary>
void Timer_T3Stop           $opc(0x66)(void);

/// <summary>Initializes Timer3 for frequency generation. Parameters are period duration and prescaler. The output signal is generated at PortE.3 (X1_13). The frequency generation is started automatically.</summary>
/// <param name="period">Period duration.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T3FRQ            $opc(0x67)(word Period,byte PS);

/// <summary>Initializes Timer3 for frequency generation. Parameters are period duration, prescaler and phase shift. The output signal is generated at PortE.3 (X1_13) and PortE.4 (X1_12). The frequency generation is started automatically. There is an extended prescaler definition for the Mega128. The phase shift must be smaller than half the period.</summary>
/// <param name="period">Period duration.</param>
/// <param name="skew">Phase shift.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T3FRQX           $opc(0x68)(word Period,word skew,byte PS);

/// <summary>Initializes Timer3 with given prescaler and pulse width. The output signal is generated at PortE.3 (X1_13).</summary>
/// <param name="period">Period duration.</param>
/// <param name="PW0">Pulse width.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T3PWM            $opc(0x69)(word Period,word PW0,byte PS);

/// <summary>Sets a new pulse width (Channel A) for Timer3 without changing the prescaler.</summary>
/// <param name="PW0">Pulse width.</param>
void Timer_T3PWA            $opc(0x6A)(word PW0);

/// <summary>Initializes Timer_3 with given period duration, prescaler, pulse width for channel A and B. The output signal is generated at PortE.3 (X1_13) and PortE.4 (X1_12).</summary>
/// <param name="period">Period duration.</param>
/// <param name="PW0">Pulse width channel A.</param>
/// <param name="PW1">Pulse width channel B.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T3PWMX           $opc(0x6B)(word Period,word PW0,word PW1,byte PS);

/// <summary>Initializes Timer_3 with given period duration, prescaler, pulse width for channel A, B and C. The output signal is generated at PortE.3 (X1_13), PortE.4 (X1_12) and PortE.5 (X1_11).</summary>
/// <param name="period">Period duration.</param>
/// <param name="PW0">Pulse width channel A.</param>
/// <param name="PW1">Pulse width channel B.</param>
/// <param name="PW2">Pulse width channel C.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T3PWMY           $opc(0x6C)(word Period,word PW0,word PW1,word PW2,byte PS);

/// <summary>Sets a new pulse width (Channel B) for Timer3 without changing the prescaler.</summary>
/// <param name="PW1">Pulse width.</param>
void Timer_T3PWB            $opc(0x6D)(word PW1);

/// <summary>Initializes Timer_3 with a prescaler and a timer interval value (16bit). After the timing interval has expired Timer_3 Interrupt is triggered. There is an extended prescaler definition for the Mega128.</summary>
/// <param name="time">Time period after which the interrupt is triggered.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T3Time           $opc(0x6E)(word Time, byte PS);

/// <summary>Defines if pulse width measurement or period measurement should be done. Then it initializes Timer_3 and sets the prescaler.</summary>
/// <param name="Mode">0 = pulse width measurement, 1 = period measurement.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T3PM             $opc(0x6F)(byte Mode, byte PS);

/// <summary>Returns the result of the measurement.</summary>
/// <returns>Result of measurement.</returns>
word Timer_T3GetPM          $opc(0x70)(void);

/// <summary>Initializes Timer_1 with given period duration, prescaler, pulse width for channel A, B and C. The output signal is generated at PortB.5 (X1_3), PortB.6 (X1_2) and PortB.7 (X1_1).</summary>
/// <param name="period">Period duration.</param>
/// <param name="PW0">Pulse width channel A.</param>
/// <param name="PW1">Pulse width channel B.</param>
/// <param name="PW2">Pulse width channel C.</param>
/// <param name="PS">Prescaler.</param>
void Timer_T1PWMY           $opc(0x71)(word Period,word PW0,word PW1,word PW2,byte PS);

#endif


/// <summary>Measures the number of 10ms ticks between two calls of Timer_TickCount(). Ignore the return value of the first call to Timer_TickCount(). If the delay between the two calls is greater than 655.36 seconds, the result is undefined.</summary>
/// <returns>Time interval expressed in 10ms ticks.</returns>
word Timer_TickCount        $opc(0x72)(void);

/// <summary>Disables the specified timer. Timer functions occupy I/O ports. If a timer is not needed and the corresponding I/O ports are used otherwise, the timer must be disabled.</summary>
/// <param name="timer">0 = Timer_0, 1 = Timer_1, 3 = Timer_3 (onlyMega128).</param>
void Timer_Disable          $opc(0x32)(byte timer);